/*
 * Created on Jan 31, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.epic.perleditor.views;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.epic.core.Constants;

/**
 * @author luelljoc
 * 
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ExplainErrorsView extends ViewPart {

    private StyledText explanationText;
    private Color red;
    private Color yellow;
    private Color black;

    private final Map<String, String> levels = new HashMap<String, String>();


    public ExplainErrorsView() {
        levels.put("(W)", "A warning (optional)");
        levels.put("(W once)", "A warning (optional)");
        levels.put("(D)", "A deprecation (optional)");
        levels.put("(S)", "A severe warning (default)");
        levels.put("(F)", "A fatal error (trappable)");
        levels.put("(P)", "An internal error you should never see (trappable)");
        levels.put("(X)", "A very fatal error (nontrappable)");
        levels.put("(A)", "An alien error message (not generated by Perl)");
    }
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    public void createPartControl(Composite parent) {
        explanationText = new StyledText(parent, SWT.MULTI | SWT.V_SCROLL
                | SWT.H_SCROLL);
        explanationText.setEditable(false);
        
        red = new Color(parent.getDisplay(), 216, 60, 26);
        yellow = new Color(parent.getDisplay(), 211, 140, 29);
        black = new Color(parent.getDisplay(), 0, 0, 0);

    }
    
    public String getText()
    {
        return explanationText.getText();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
     */
    public void setFocus() {
        explanationText.setFocus();
    }
    
    public void explain(List<IMarker> markers) {
        explanationText.setText("");
        
        for(int i=0; i<markers.size(); i++) {
            IMarker marker = markers.get(i);
            try {
                int pos = explanationText.getCharCount();
                explanationText.append((String) marker.getAttribute(IMarker.MESSAGE) +"\n");
                
                int severity = ((Integer) marker.getAttribute(IMarker.SEVERITY)).intValue();
                
                StyleRange styleRange = new StyleRange();
                styleRange.start = pos;
                styleRange.length = ((String) marker.getAttribute(IMarker.MESSAGE)).length();
                styleRange.fontStyle = SWT.BOLD;
                styleRange.foreground = severity == IMarker.SEVERITY_WARNING ? yellow : red;
                explanationText.setStyleRange(styleRange);
                
                
                String expStr = (String) marker.getAttribute(Constants.MARKER_ATTR_PERL_ERROR_EXPLANATION);
                
                String levelStr = null;
                
                if(expStr.length() > 0) {
                    levelStr = levels.get(expStr.substring(0,expStr.indexOf(')')+1));
                }
                
                if(levelStr != null) {
                    pos = explanationText.getCharCount();
                    explanationText.append(levelStr +"\n");
                    
                    styleRange = new StyleRange();
                    styleRange.start = pos;
                    styleRange.length = levelStr.length();
                    styleRange.fontStyle = SWT.ITALIC;
                    styleRange.foreground = black;
                    explanationText.setStyleRange(styleRange);
                }

                
                explanationText.append(levelStr != null ? expStr.substring(expStr.indexOf(')')+2) : expStr);
                explanationText.append("\n");
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }
    }

}